package short_drama

import (
	"context"
	"errors"
	v1 "video-api/api/v1"
	"video-api/internal/model/out"
	"video-api/internal/service"
)

type shortDramaController struct{}

var ShortDramaHandle = new(shortDramaController)

// GetShortDramasList 短剧列表
func (c *shortDramaController) GetShortDramasList(ctx context.Context, req *v1.ShortDramasReq) (res *out.Paging, err error) {
	list, count, err := service.ShortDramaService().GetList(ctx, &req.ShortDramaList)
	if err != nil {
		return nil, err
	}
	//res = new(v1.ShortDramasRes)
	//res.List = list
	return out.FillPaging(count, req.Page, req.PageSize, list), nil
}

// GetShortDramaDetail 短剧详情
func (c *shortDramaController) GetShortDramaDetail(ctx context.Context, req *v1.ShortDramaDetailReq) (res *out.ShortDramaEntity, err error) {
	drama, err := service.ShortDramaService().GetDetail(ctx, &req.ShortDramaDetail)
	if err != nil {
		return nil, err
	}
	res = new(out.ShortDramaEntity)
	res = drama
	return
}

// ShortDramaEpisodesPlayEvent 短剧播放事件
func (c *shortDramaController) ShortDramaEpisodesPlayEvent(ctx context.Context, req *v1.ShortDramaEpisodesPlayEventReq) (res *v1.ShortDramaEpisodesPlayEventRes, err error) {
	res = &v1.ShortDramaEpisodesPlayEventRes{}
	err = service.ShortDramaService().EpisodesPlayEvent(ctx, &req.ShortDramaEpisodesPlayEvent)
	if err != nil {
		return res, err
	}
	return res, nil
}

// CollectShortDrama 收藏短剧
func (c *shortDramaController) CollectShortDrama(ctx context.Context, req *v1.ShortDramaCollectReq) (res *v1.ShortDramaCollectRes, err error) {
	isCollect, err := service.ShortDramaService().Collect(ctx, &req.ShortDramaCollect)
	if err != nil {
		return nil, err
	}
	res = new(v1.ShortDramaCollectRes)
	res.IsCollect = isCollect
	return
}

// GetCollectShortDramaList 短剧收藏列表
func (c *shortDramaController) GetCollectShortDramaList(ctx context.Context, req *v1.ShortDramaCollectListReq) (res *v1.ShortDramaCollectListRes, err error) {
	list, err := service.ShortDramaService().GetCollectList(ctx, &req.ShortDramaCollectList)
	if err != nil {
		return nil, err
	}
	res = new(v1.ShortDramaCollectListRes)
	res.List = list
	return
}

// GetHistoryShortDramaList 历史短剧列表
func (c *shortDramaController) GetHistoryShortDramaList(ctx context.Context, req *v1.ShortDramaHistoryListReq) (res *out.Paging, err error) {
	list, count, err := service.ShortDramaService().GetHistoryList(ctx, &req.ShortDramaHistoryList)
	if err != nil {
		return nil, err
	}
	return out.FillPaging(count, req.Page, req.PageSize, list), nil
}

func (c *shortDramaController) RmHistory(ctx context.Context, req *v1.ShortDramaHistoryRemReq) (bool, error) {
	res, err := service.ShortDramaService().RmHistoryRecord(ctx, req.Id)
	if err != nil {
		return res, err
	}
	return true, nil
}

// GetShortDramaEpisodeById 获取单集剧集信息
func (c *shortDramaController) GetShortDramaEpisodeById(ctx context.Context, req *v1.ShortDramaEpisodeReq) (res *out.ShortDramaEpisodesDetail, err error) {
	episodes, err := service.ShortDramaService().GetShortDramaEpisodeById(ctx, &req.ShortDramaEpisodes)
	if err != nil {
		return nil, err
	}
	if episodes.Id == 0 {
		return nil, errors.New("剧集不存在")
	}
	return episodes, nil
}
